﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using EntityModel;
namespace Services
{
    public class ProductAlbumService
    {
        #region***属性数据***

        ShopDbEntities bdContext = DbContextHelper.GetCurrentDbContext();
        ProductService productService = new ProductService();

        #endregion

        #region 获取所有分类

        /// <summary>
        /// 获取所有分类
        /// </summary>
        /// <returns></returns>
        ///  PagedList<Category>
        ///  
        public IQueryable<ProductAlbum> GetAllList(int pid)
        {
            return bdContext.ProductAlbums.Where(p => p.PruductId == pid).OrderByDescending(p => p.DisplayOrder);
        }

        #endregion

        #region 获取所有显示产品相册

        /// <summary>
        /// 获取所有显示产品相册
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ProductAlbum> GetList()
        {
            return bdContext.ProductAlbums.OrderByDescending(c => c.DisplayOrder);
        }
        #endregion

        #region 新增数据
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="model"></param>
        public void Create(ProductAlbum model)
        {
            bdContext.ProductAlbums.AddObject(model);
            bdContext.SaveChanges();
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            ProductAlbum album = bdContext.ProductAlbums.FirstOrDefault(p => p.Id == id);
            bdContext.ProductAlbums.DeleteObject(album);
            bdContext.SaveChanges();

        }
        #endregion

        #region 设置产品相册封面
        /// <summary>
        /// 设置产品相册封面
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="albumId"></param>
        public void IsHome(int pid, int albumId)
        {
            ProductAlbum album = bdContext.ProductAlbums.FirstOrDefault(p => p.Id == albumId);
            album.IsHome = true;
            bdContext.SaveChanges();
            productService.SetAlbumHome(pid, albumId);
        }
        #endregion
    }
}
